// server/database/migrations/2025110603_create_inbound_orders_table.js
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建入库单表
    await queryInterface.createTable('inbound_orders', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      inboundNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '入库单号'
      },
      supplierId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '供应商ID'
      },
      inboundDate: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '入库日期'
      },
      totalAmount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        defaultValue: 0,
        comment: '总金额'
      },
      hasInvoice: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '是否含票'
      },
      invoiceStatus: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '发票状态: pending(待开票), received(已收票), uploaded(已上传)'
      },
      freight: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0,
        comment: '运费'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    });

    // 创建入库单明细表
    await queryInterface.createTable('inbound_order_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      inboundOrderId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '入库单ID'
      },
      productId: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '商品ID'
      },
      productName: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '商品名称'
      },
      specification: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '规格'
      },
      quantity: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '数量'
      },
      unitPrice: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0,
        comment: '单价'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    });

    // 添加索引
    await queryInterface.addIndex('inbound_orders', ['inboundNumber'], {
      name: 'inbound_orders_idx_inbound_number',
      unique: false
    });
    await queryInterface.addIndex('inbound_orders', ['supplierId'], {
      name: 'inbound_orders_idx_supplier_id',
      unique: false
    });
    await queryInterface.addIndex('inbound_orders', ['inboundDate'], {
      name: 'inbound_orders_idx_inbound_date',
      unique: false
    });
    await queryInterface.addIndex('inbound_orders', ['invoiceStatus'], {
      name: 'inbound_orders_idx_invoice_status',
      unique: false
    });
    await queryInterface.addIndex('inbound_order_items', ['inboundOrderId'], {
      name: 'inbound_order_items_idx_inbound_order_id',
      unique: false
    });
    await queryInterface.addIndex('inbound_order_items', ['productId'], {
      name: 'inbound_order_items_idx_product_id',
      unique: false
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 删除索引
    await queryInterface.removeIndex('inbound_orders', 'inbound_orders_idx_inbound_number');
    await queryInterface.removeIndex('inbound_orders', 'inbound_orders_idx_supplier_id');
    await queryInterface.removeIndex('inbound_orders', 'inbound_orders_idx_inbound_date');
    await queryInterface.removeIndex('inbound_orders', 'inbound_orders_idx_invoice_status');
    await queryInterface.removeIndex('inbound_order_items', 'inbound_order_items_idx_inbound_order_id');
    await queryInterface.removeIndex('inbound_order_items', 'inbound_order_items_idx_product_id');
    
    // 删除表
    await queryInterface.dropTable('inbound_order_items');
    await queryInterface.dropTable('inbound_orders');
  }
};